# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import pymysql
from amazon import items
from icecream import ic
from scrapy.exceptions import DropItem

class AmazonPipeline:

    def open_spider(self, spider):
        config = spider.settings['DATA_CONFIG']
        if config['type'] == 'mysql':
            # 连接数据库   **config['config'] 解包操作
            self.conn = pymysql.connect(**config['config'])
            self.cursor = self.conn.cursor()
            spider.conn = self.conn
            spider.cursor = self.cursor

    def close_conn(self, spider):
        config = spider.settings('DATA_CONFIG')
        if config['type'] == 'mysql':
            self.cursor.close()
            self.conn.close()

    def process_item(self, item, spider):
        if isinstance(item, items.AmazonItem):
            try:
                sql = 'insert into caty (name,url) values (%s,%s)'
                self.cursor.execute(sql, (
                    item['name'],
                    item['url']
                ))
                ic(item['name'],'=====>>来自pipelines')
                self.conn.commit()
            except Exception as e:
                self.conn.rollback()
                print('信息写入错误%s-%s' %(item['url'], e))
        elif isinstance(item, items.DetailItem):
            try:
                sql = 'insert into detail (title,author,start,price,c_id) values (%s,%s,%s,%s,%s)'
                if isinstance(item,items.DetailItem):
                    self.cursor.execute(sql,(
                        item['title'],
                        item['author'],
                        item['start'],
                        item['price'],
                        item['c_id']
                    ))
                    self.conn.commit()
                    ic(item['start'],'========>>来自pipeline 的详情解析')
            except Exception as e:
                self.conn.rollback()
                ic(fr'详情信息写入错误,{item["url"]}--{e}')
        else :
            raise DropItem
        return item
